﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using ServerBase.Protocol;
using static ServerGame.Service.Dispatcher;
using static ServerBase.Config.Conf;
using ServerBase.Service;
using static ServerBase.Service.BaseDispatch;
using ServerBase.Config;
using ServerPublic.Manager;
using System;
using ServerGame.GameObject;
using UtilPublic.ApiDispatcher;
using ServerPublic.Data;
using ServerPublic.PubObject;
using ServerPublic.DataBaseMysql;
using UtilPublic.ServerLoger;
using UtilPublic.Serialization;

namespace ServerGame.Manager
{
    public partial class DispatcherMsg
    {
        public static readonly bool 快速注册账号 = EGlobalId.快速注册账号.ValueBool();
        //注册用户
        public static User UserRegister(string account, string password)
        {
            long uuid = IdMgr.CreateUuid();

            var user = new User();
            user.Uuid = uuid;
            user.Account = account;
            user.Password = password;
            user.TimeRegister = DateTime.Now;
            return user;
        }
        [ApiDpPro((short)EProtocolId.C2L_LOGIN_USERLOGIN, "登陆用户账号")]
        public short OnLoginUserLogin()
        {
            (var Req, var Rsp) = ThisReq.Body.OnLoginUserLogin();

            loger.Debug($"Account {Req.Account} Password {Req.Password}");
            if (PubUserMgr.CheckRestrictLogin(ThisSession.IpAddress()))
            {
                Rsp.Shuttle = PubDataAllMgr.RestrictLogin;
                Rsp.Result = EProtocolResult.登陆受限; return Send(Rsp);
            }

            if (BaseServerInfo.ServerRunMode == EServerRunMode.正式发布)
            {
                if (Req.Pin != BaseServerInfo.CfgPlatform.VersionPin)
                {
                    Rsp.Result = EProtocolResult.游戏版本不一致; return Send(Rsp);
                }
            }

            if (Req.Account.Length <= 0)
            {
                Rsp.Result = EProtocolResult.账号长度不符; return Send(Rsp);
            }
            User user = null;

            if (快速注册账号)
            {
                //查询账号是否已注册
                if (PubUserMgr.GetUser(Req.Account, out user))
                {
                    //密码错误
                    if (Req.Password != user.Password)
                    {
                        Rsp.Result = EProtocolResult.密码错误; return Send(Rsp);
                    }
                }
                else
                {
                    if ((user = UserRegister(Req.Account, Req.Password)) == null)
                    {
                        Rsp.Result = EProtocolResult.服务器数据库异常; return Send(Rsp);
                    }
                    (ERecordType.注册账号, EActionSource.系统).Rec(0, Req.Account, Req.Password);
                }
            }
            else
            {
                //查询账号是否已注册
                if (user == null)
                {
                    Rsp.Result = EProtocolResult.账号不存在; return Send(Rsp);
                }
                //密码错误
                if (Req.Password != user.Password)
                {
                    Rsp.Result = EProtocolResult.密码错误; return Send(Rsp);
                }
            }

            user.TimeLogin = DateTime.Now;

            if (!user.Save())
            {
                Rsp.Result = EProtocolResult.服务器数据库异常; return Send(Rsp);
            }

            //设置连接ID类型
            ThisSession.SessionType = 0;

            //组装消息
            Rsp.Uuid = user.Uuid;
            Rsp.Code = Util.SimpleVerifyCode(user.Uuid);

            PubDataLoadMgr.GetDictServer(user.Uuid, ref Rsp.DictServer);

            return Send(Rsp);
        }
    }
}